namespace SimpleWebServices.Web.UI.Html.Validation
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    [XmlRoot("validators")]
    public sealed class ValidatorSettingsCollection : IEnumerable<ValidatorSettings>, IXmlSerializable
    {
        public ValidatorSettingsCollection()
        {
            this.Items = new Collection<ValidatorSettings>();
        }

        public int Count
        {
            get
            {
                return this.Items.Count;
            }
        }

        private Collection<ValidatorSettings> Items
        {
            get;
            set;
        }

        public ValidatorSettings this[string input]
        {
            get
            {
                return this.Items.Where(x => string.Equals(input, x.For, StringComparison.Ordinal)).FirstOrDefault();
            }
        }

        public ValidatorSettings this[int index]
        {
            get
            {
                return this.Items[index];
            }

            set
            {
                if (null == value)
                {
                    throw new ArgumentNullException("value");
                }

                this.Items[index] = value;
            }
        }

        public void Add(ValidatorSettings item)
        {
            if (null == item)
            {
                throw new ArgumentNullException("item");
            }

            this.Items.Add(item);
        }

        public void Clear()
        {
            this.Items.Clear();
        }

        IEnumerator<ValidatorSettings> IEnumerable<ValidatorSettings>.GetEnumerator()
        {
            return (this.Items as IEnumerable<ValidatorSettings>).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (this.Items as IEnumerable).GetEnumerator();
        }

        XmlSchema IXmlSerializable.GetSchema()
        {
            throw new NotSupportedException();
        }

        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (null == reader)
            {
                throw new ArgumentNullException("reader");
            }

            if (reader.IsEmptyElement)
            {
                reader.Read();
            }
            else
            {
                string name = reader.Name;
                reader.Read();
                while (true)
                {
                    if (XmlNodeType.None == reader.NodeType)
                    {
                        break;
                    }
                    else if (XmlNodeType.EndElement == reader.NodeType && reader.Name == name)
                    {
                        reader.Read();
                        break;
                    }
                    else if ("validator".Equals(reader.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        this.Items.Add(reader.ReadOuterXml().Deserialize<ValidatorSettings>());
                    }
                    else
                    {
                        reader.Read();
                    }
                }
            }
        }

        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            if (null == writer)
            {
                throw new ArgumentNullException("writer");
            }

            foreach (ValidatorSettings item in this.Items)
            {
                writer.WriteRaw(item.Serialize().CreateNavigator().OuterXml);
            }
        }
    }
}